<template>
  <div>
    <div class="box">
      <div class="head">
        <div class="input">
          <span>文章标题：</span>
          <el-input v-model="queryData.title" placeholder="请输入文章标题" style="width: 200px"></el-input>
        </div>
        <div class="input">
          <span>文章分类：</span>
          <el-select v-model="queryData.type_id" placeholder="请选择分类" style="width: 200px">
            <el-option label="全部分类" :value="0"></el-option>
            <el-option
              v-for="item in categoryList"
              :key="item.id"
              :label="item.type_name"
              :value="item.id"
            ></el-option>
          </el-select>
        </div>
        <el-button type="primary" @click="search">搜索</el-button>
        <el-button @click="reset">重置</el-button>
      </div>
      <el-table
        :data="tableData"
        border
        v-loading="isTableLoading"
        empty-text="暂无数据"
        style="width: 100%"
        :max-height="tableHeight"
      >
        <el-table-column fixed type="index" align="center" label="序号" width="60" />
        <el-table-column prop="title" align="center" label="文章标题" min-width="200" show-overflow-tooltip />
        <el-table-column prop="type_name" align="center" label="文章分类" width="120" />
        <el-table-column prop="desc" align="center" label="文章描述" min-width="200" show-overflow-tooltip />
        <el-table-column prop="img_url" align="center" label="封面图片" width="120">
          <template #default="scope">
            <el-image
              v-if="scope.row.img_url"
              :src="scope.row.img_url"
              style="height: 50px; width: auto; max-width: 80px;"
              :preview-src-list="[scope.row.img_url]"
              alt="封面图片"
            ></el-image>
            <span v-else style="color: #999; font-size: 12px;">暂无图片</span>
          </template>
        </el-table-column>
        <el-table-column prop="see_total" align="center" label="阅读量" width="100" />
        <el-table-column prop="is_top" align="center" label="是否置顶" width="100">
          <template #default="scope">
            <el-tag :type="scope.row.is_top ? 'success' : 'info'">
              {{ scope.row.is_top ? '置顶' : '普通' }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="from_type_text" align="center" label="来源" width="100" />
        <el-table-column prop="create_time" align="center" label="创建时间" width="160" />
        <el-table-column fixed="right" align="center" label="操作" width="120">
          <template #default="scope">
            <div class="action">
              <span @click="viewDetail(scope.row)">查看详情</span>
            </div>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        background
        style="margin-top: 5px; text-align: right"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page.sync="queryData.page"
        :page-size.sync="queryData.size"
        layout="total, prev, pager, next, sizes, jumper"
        :total="total"
      >
      </el-pagination>
    </div>
  </div>
</template>

<script>
import { ArticleList, ArticleTypes } from '@/api/manage';

export default {
  name: 'article',
  data() {
    return {
      tableHeight: 500,
      queryData: {
        page: 1,
        size: 10,
        type_id: 0,
        title: '',
      },
      total: 0,
      isTableLoading: false,
      tableData: [],
      categoryList: [],
    };
  },
  mounted() {
    this.$nextTick(() => {
      this.tableHeight = window.innerHeight - 260;
    });
    this.getList();
    this.getCategoryList();
  },
  methods: {
    search() {
      this.queryData.page = 1;
      this.getList();
    },
    reset() {
      this.queryData.title = '';
      this.queryData.type_id = 0;
      this.search();
    },
    handleSizeChange(size) {
      this.queryData.size = size;
      this.getList();
    },
    handleCurrentChange(page) {
      this.queryData.page = page;
      this.getList();
    },
    getList() {
      this.isTableLoading = true;
      const params = { ...this.queryData };
      if (params.type_id === 0) {
        delete params.type_id;
      }
      ArticleList(params).then((res) => {
        this.tableData = res.data.list;
        this.total = res.data.total;
        this.isTableLoading = false;
      }).catch(() => {
        this.isTableLoading = false;
      });
    },
    getCategoryList() {
      ArticleTypes().then((res) => {
        this.categoryList = res.data.list;
      });
    },
    viewDetail(item) {
      this.$router.push({
        path: '/company/articleDetail/index',
        query: { id: item.id }
      });
    },
  },
};
</script>

<style scoped>
.head {
  width: 100%;
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  margin-bottom: 20px;
}
.input {
  display: flex;
  align-items: center;
  margin-right: 15px;
  margin-bottom: 10px;
}
.input > span {
  font-size: 12px;
  flex-shrink: 0;
  margin-right: 8px;
}
.box {
  width: 100%;
  background: #ffffff;
  border-radius: 5px;
  padding: 20px;
}
.action {
  display: flex;
  align-items: center;
  justify-content: center;
}
.action span {
  color: #0256ff;
  font-size: 12px;
  cursor: pointer;
  margin: 0 8px;
}
</style> 